package com.example.guet_assistant.controller;
import com.example.guet_assistant.constant.MessageConstant;
import com.example.guet_assistant.result.Result;
import com.example.guet_assistant.utils.AliOssUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Tag(name="通用接口")
@RestController
@RequestMapping("/common")
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;
    @Operation(summary="文件上传")
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file){
        try {
            //获取原始文件名
            String originalFilename = file.getOriginalFilename();
            //获取扩展文件名
            String extention = originalFilename.substring(originalFilename.lastIndexOf("."));
            //生成ObjectName
            String  ObjectName= UUID.randomUUID().toString()+extention;
            //返回图片在阿里云的请求路径
            String filePath = aliOssUtil.upload(file.getBytes(), ObjectName);
            return Result.success(filePath);
        } catch (IOException e) {
            return  Result.success(MessageConstant.UPLOAD_FAILED);
        }

    }
}
